package com.it.myd.controller.main.user;

import com.it.myd.bean.main.ReturnValue;
import com.it.myd.bean.main.User;
import com.it.myd.service.main.LoginService;
import com.it.myd.service.main.user.UserChangerService;
import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import jakarta.servlet.http.Part;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.File;
import java.io.IOException;

@Controller
@RequestMapping("/user/changer")
public class UserChangerController {

    @Autowired
    private UserChangerService userCService;
    @Autowired
    private LoginService loginService;

    @RequestMapping("userimg")
    @ResponseBody
    public ReturnValue ajaxUploadImg(HttpServletRequest request) throws IOException, ServletException {
        ServletContext application = request.getServletContext();
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        //设置请求的编码格式
        request.setCharacterEncoding("UTF-8");
        //获取Part对象
        Part part=request.getPart("file");//表单中file文件域的name属性值
        //通过Part对象得到上传的文件名
        String filename=part.getSubmittedFileName();
        if(user.getUser_img().getFile_name().equals("undefined.jpg")){
            //得到文件存放的路径
            String filePath="/usr/myd/img/userimg/"+user.getUser_id();
            File uploadedFile= new File(filePath);
            if(!uploadedFile.exists()){
                uploadedFile.mkdir();
            }
            //上传文件到指定目录
            part.write(filePath+"/"+filename);
            user.getUser_img().setFile_name(filename);
            user.getUser_img().setFile_path("/userimg/"+user.getUser_id()+"/");

            userCService.updateUserProFile(user);
        }else{
            String filePath="/usr/myd/img/userimg/"+user.getUser_id()+filename;
            File uploadedFile= new File(filePath);
            uploadedFile.delete();
            filePath="/usr/myd/img/userimg/"+user.getUser_id();
            part.write(filePath+"/"+filename);
            user.getUser_img().setFile_name(filename);
            userCService.updateUserProFile(user);
        }
        session.setAttribute("user",user);
        ReturnValue<String> ret = new ReturnValue<>();
        ret.isSuccess();
        return ret;
    }

    @RequestMapping("username")
    @ResponseBody
    public ReturnValue changerUserName(HttpServletRequest request){
        ServletContext application = request.getServletContext();
        HttpSession session = request.getSession();
        String username = request.getParameter("username");
        User user = (User) session.getAttribute("user");
        user.setUser_name(username);
        userCService.updateUserName(user);
        ReturnValue<String> ret = new ReturnValue<>();
        ret.isSuccess("修改成功");
        return ret;
    }

    @RequestMapping("usersex")
    @ResponseBody
    public ReturnValue changerUserSex(HttpServletRequest request){
        ServletContext application = request.getServletContext();
        HttpSession session = request.getSession();
        String usersex = request.getParameter("usersex");
        User user = (User) session.getAttribute("user");
        user.setUser_sex(usersex);
        userCService.updateUserSex(user);
        User user_t = loginService.CheckUser(user);
        user_t.setUser_img(user.getUser_img());
        user.setUser_sex(user_t.getUser_sex());
        ReturnValue<String> ret = new ReturnValue<>();
        ret.isSuccess("修改成功");
        return ret;
    }

    @RequestMapping("usergrjj")
    @ResponseBody
    public ReturnValue changerUserGrjj(HttpServletRequest request){
        ServletContext application = request.getServletContext();
        HttpSession session = request.getSession();
        String usergrjj = request.getParameter("userjj");
        User user = (User) session.getAttribute("user");
        user.setUser_grjj(usergrjj);
        userCService.updateUserGrjj(user);
        User user_t = loginService.CheckUser(user);
        user_t.setUser_img(user.getUser_img());
        user.setUser_grjj(user_t.getUser_grjj());
        ReturnValue<String> ret = new ReturnValue<>();
        ret.isSuccess("修改成功");
        return ret;
    }

}
